/**
 * LoginPage View
 * @author helio
 */
if (!SALE.LoginPage) SALE.LoginPage = Ext.extend(Ext.Panel, {
        layout: 'border',		
		border: false,
        initComponent: function(){
        	
            this.loginForm = new Ext.FormPanel({
				region: 'center',
                labelWidth: 60,
				bodyStyle:'padding-left:20px; margin:123px 0 0;',
                items: {
                    xtype: 'fieldset',
                    autoHeight: true,
					border : false,
                    anchor: '80%',
                    layout: 'form',
                    defaultType: 'textfield',
                    items: [{
                        name: 'loginid',
                        allowBlank: false,
                        anchor: '92%'
                    }, {
                        inputType: 'password',
                        name: 'password',
						style:'margin-top:8px',
						allowBlank: false,
                        anchor: '92%'
                    }, {
                        xtype: 'radiogroup',
                        anchor: '95%',
                        style:'margin-top:10px', 
                        items: [{
                            boxLabel: '单机版',
                            name: 'status',
                            inputValue: 'local',
                            listeners: {
                                check: this.radioCheckHandler.createDelegate(this)
                            }
                        }, {
                            boxLabel: '网络版',
                            name: 'status',
                            inputValue: 'remote',
                            checked: true,
                            listeners: {
                                check: this.radioCheckHandler.createDelegate(this)
                            }
                        }, ]
                    }, {
						xtype:"label",
						id:"loginMessage",
						cls:"messageLabel",
					}, {
						xtype: "panel",
						border: false,
						anchor: '96%',
						buttons: [{
		                    text: '登录',
		                    iconCls: 'btn-login',					
		                	scale: 'medium',
		                    listeners: {
		                        "click": this.loginHandler.createDelegate(this)
		                    }
		                }]
					}],
					keys:{
						key: Ext.EventObject.ENTER,
						fn: this.loginHandler.createDelegate(this)
					}
                }
            });
			
			this.linkPanel = new Ext.Panel({
				region: 'south',
				height: 175,
				
				items: [{
					height: 62,
					border: false,
					bodyStyle: "padding: 12px 100px 0 40px",
					html: "<div style='width:100%;height:100%;cursor:pointer;' id='registerlink'></div>"
				}, {
					height: 62,
					border: false,
					bodyStyle: "padding: 13px 100px 0 40px",
					html: "<div style='width:100%;height:100%;cursor:pointer;' id='portallink'></div>",		
				}],
				
				listeners: {
					afterlayout: function() {
						document.getElementById("registerlink").onclick = this.showRegisterWindow;
						document.getElementById("portallink").onclick = this.showHomePage;
					}.createDelegate(this)
				}
			});
            
            this.items = {
                region: 'center',
                frame: true,
                layout: 'border',
                items: [{
                    region: 'north',
                    border: false,
					height: 60,
                }, {
                    region: 'west',
                    border: true,
                    width: 80,			
					cls : 'login-left-panel',
                }, {
                    region: 'center',
                    border: false,
					//id: 'showMessagePanel',
					cls : 'login-center-panel',
					bodyStyle: "padding: 40px 0 0",
                    autoLoad: {
                        url: "html/show.html"                    
                    }
                }, {
                    region: 'east',
                    border: true,
                    width: 300,					
					cls : 'login-right-panel',
					layout: 'border',
                    items: [this.loginForm, this.linkPanel]
                }, {
                    region: 'south',
                    border: false,
					height: 60,
                    autoLoad: {
                        url: "html/footer.html"
                    }
                }]
            };
	
            SALE.LoginPage.superclass.initComponent.call(this);

            //还原到原始窗口大小
			nativeWindow.restore();
        },
        
		radioCheckHandler : function(checkradio, checked) {
			if (checked) {
            	SALE.App.loginType = checkradio.inputValue;
            }
			/*
			if(this.loginType == "remote") {
				Ext.get('showMessagePanel').load({
                	url: "html/show.html"
                });
			} else {
				Ext.get('showMessagePanel').load({
                	url: "html/show.html"
                });
			}*/
		},
		
        loginHandler: function(){ 
			
			if(this.loginForm.form.isValid()) {
				//设置本地登录还远程登录。
				SALE.App.loginType = this.loginForm.form.getValues()['status'];
				
				if(SALE.App.loginType == "remote") {
					Ext.MessageBox.show({
						msg: '正在验证用户信息，请等待...',
				        progressText: 'Saving...',
				        width:300,
				        wait:true,
				        waitConfig: {interval:200},
				        icon:'ext-mb-download', //custom class in msg-box.html
				        animEl: 'loginBtn'
				    });
					
					setTimeout(function(){
						SALE.Action.loginAction(this.showErrorMessage,this.loginForm.form);
        			}.createDelegate(this), 200);
					
				} else {
					SALE.Action.loginAction(this.showErrorMessage,this.loginForm.form);
				}				
			}
			
        },
        
        showErrorMessage : function() {
        	if(SALE.App.userInfo && !SALE.App.userInfo.success) {
				var label = Ext.getCmp("loginMessage");
				if(label) {
					label.setText(SALE.App.userInfo.message);
				}
			} 
        },
        
		showRegisterWindow: function() {
        	this.inputWin = new Ext.Window({
		 		
    		 	title: '新用户注册申请',
    			closable: true,
    			frame: true,
    			//plain:true,
    			width: 680,
    			height: 500,
    			layout: 'border',
    			closeAction :'close',
    			bodyStyle: "padding:10px;",	
    			items: [{
    				region: 'north',
    				border: false,
    				bodyStyle : 'background-color: transparent;', 
					height: 60,
                    autoLoad: {
                        url: "html/register_header.html"
                    }
    			}, {
        			region: 'center',
        			margins: '20 20 20 20',
    				xtype : "companyregister"
    			}]
    		});
    		
    		this.inputWin.show();
		},
		
		showHomePage: function() {
			SALE.Utils.navigateToURL(SALE.App.configInfo.home);
		}
    });

Ext.reg('loginpage', SALE.LoginPage);